<?php
namespace Admin\Controller;
use Admin\Controller;
/**
 * 文章管理
 */
class NewsController extends BaseController
{
    /**
     * 文章列表
     * @return [type] [description]
     */
    public function index($key="")
    {
//        if($key == ""){
//            $model = D('Article');
//        }else{
//            $where['title'] = array('like',"%$key%");
//            $where['_logic'] = 'or';
//            $model = D('Article')->where($where);
//        }
//
//        $count  = $model->where($where)->count();// 查询满足要求的总记录数
//        $Page = new \Extend\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
//        $show = $Page->show();// 分页显示输出
//        $article = $model->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('id DESC')->select();
//        $this->assign('model', $article);
//        $this->assign('page',$show);\
        $model = D("News");
        $data=$model->select();
        foreach ($data as $v){
            $v['add_time']=date("Y-m-d h:i:sa", $v['add_time']);
        }
        $this->assign('data', $data);
        $this->display();     
    }
    /**
     * 添加文章
     */
    public function add()
    {
        //默认显示添加表单
        if (!IS_POST) {
        	$this->assign("category",getSortedCategory(M('category')->select()));
            $this->display();
        }
        if (IS_POST) {
            //如果用户提交数据
            $model = D("News");

            $_POST['add_time']=time();
            $data=$_POST;
            if ($model->add($_POST)) {
                $this->success("添加成功", U('news/index'));
            } else {
                $this->error("添加失败");
            }

        }
    }
    /**
     * 更新文章信息
     * @param  [type] $id [文章ID]
     * @return [type]     [description]
     */
    public function update($id)
    {
        //默认显示添加表单
        if (!IS_POST) {
            $model = M("news")->where('news_id='.$id)->find();
            $this->assign('article',$model);
            $this->display();
        }
        if (IS_POST) {
            $model = D("news");
                if ($model->save($_POST)) {
                    $this->success("更新成功", U('news/index'));
                } else {
                    $this->error("更新失败");
                }

        }
    }
    /**
     * 删除文章
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delete($id)
    {
        $model =D("news");
        $result = $model->where("news_id=".$id)->delete();
        if($result){
            $this->success("删除成功", U('news/index'));
        }else{
            $this->error("删除失败");
        }
    }
    public function  detail(){
        $id=I('get.id')+0;
        if($id>0 && is_numeric($id)){
            $model = D("News");
            $data=$model->find($id);
            $this->assign('data',$data);
            $this->display();
        }
    }
}
